Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update return type for jwt.encode #521

Merged
merged 1 commit into from
Oct 15, 2020
Merged

Conversation

moomoolive
Copy link
Contributor

It seems to me that the return type for the encode method is actually 'bytes' type and not 'str'. While this doesn't make a huge difference working with python code, it gave me some issues yesterday when sending JWT tokens between a Python (flask) REST API and Javascript (Vue) frontend.

It seems to me that the return type for the encode method is actually 'bytes' type and not 'str'. While this doesn't make a huge difference working with python code, it gave me some issues yesterday when sending JWT tokens between a Python (flask) REST API and Javascript (Vue) frontend.
Copy link
Collaborator

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you share what errors you are facing?

@moomoolive
Copy link
Contributor Author

moomoolive commented Oct 12, 2020

It was honestly pretty simple, since the 'jwt.encode' method outputs a bytes object and not a string, I ended up sending something that looked like this to my front end application:

b'the_jwt_token'

instead of just the token itself.

The problem arose when I tried to use the token for authorization (decode it), where it would throw an 'invalid header padding warning' because I was sending the bytes object of the token, instead of the string of the token itself.

Hope that makes sense.

@auvipy auvipy merged commit 96ec863 into jpadilla:master Oct 15, 2020
@tlinhart
Copy link

@moomoolive @auvipy I think this PR should be reverted as it seems that #513 actually made encode return a string. PyJWT.encode() uses its base class' encode method which returns string, see

return encoded_string.decode("utf-8")
.

@moomoolive
Copy link
Contributor Author

moomoolive commented Oct 23, 2020

@tlinhart although the line of code you referenced above should have solved the problem it doesn't seem to, at least when I try it in my terminal. (BTW I'm using the latest distribution of library 1.7.1)

image

It only becomes a string when I decode the token with ASCII.

image

Maybe decoding needs to be in ASCII??

@tlinhart
Copy link

Well, in 1.7.1 it returns bytes, but not in master. The current (master) version returns str and thus this PR (which was merged into master 8 days ago) should be reverted.

@rayluo
Copy link
Contributor

rayluo commented Jan 15, 2021

@tlinhart was right on this:

Well, in 1.7.1 it returns bytes, but not in master. The current (master) version returns str and thus this PR (which was merged into master 8 days ago) should be reverted.

And, to document my investigation findings here: even though this PR was mentioned in the v2.0.0 release notes and changelogs, but that line was indeed reverted subsequently. In other words, encode(...) returns a string, not bytes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants